package org.example.wikwok.controller;

import io.jsonwebtoken.Claims;
import org.example.wikwok.common.JwtProperties;
import org.example.wikwok.common.JwtUtil;
import org.example.wikwok.pojo.Result;
import org.example.wikwok.service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

//处理视频搜索的HTTP请求。
@RestController
@RequestMapping("/search")
public class SearchController {
    @Autowired
    private SearchService searchService;
    @RequestMapping(value = "/video",method = RequestMethod.GET)
    public Result<Object> search(String token,String keyword) {
        if(token == null || token.isEmpty()){
            return Result.buildFailure(8888);
        }
        Claims claims = JwtUtil.parseJWT(JwtProperties.getUserSecretKey(), token);
        int userId = Integer.parseInt(claims.get("userId").toString());
        //调用搜索引擎搜索关键字，并返回搜索结果。
        try{
            return Result.ok(searchService.search(userId,keyword),4001);
        } catch (Exception e) {
            return Result.buildFailure(4002);
        }
    }
}
